//
//  WKLoginViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/2.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKLoginViewController.h"
#import "WKBaseHeader.h"

@interface WKLoginViewController ()

@property (weak, nonatomic) IBOutlet UIView *contentView;//内容view
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;//手机号
@property (weak, nonatomic) IBOutlet UITextField *password;//密码
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *resetPasswordBtn;//忘记密码按钮
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;//注册按钮

@end

@implementation WKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
}

- (void)layoutUI {
    self.contentView.layer.cornerRadius = 10;//切圆角
    self.contentView.layer.shadowOpacity = 0.5;//阴影透明度
    self.contentView.layer.shadowColor = [UIColor grayColor].CGColor;//阴影的颜色
    self.contentView.layer.shadowRadius = 3;//阴影扩散的范围控制
    self.contentView.layer.shadowOffset = CGSizeMake(1, 1);//阴影的范围
    
    self.loginBtn.backgroundColor = AppManager.themeColor;
    self.loginBtn.titleLabel.font = AppManager.btnFont;
    self.loginBtn.layer.cornerRadius = self.loginBtn.frame.size.height/2;
    self.loginBtn.layer.masksToBounds = YES;
    
    self.resetPasswordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.registerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}

- (IBAction)clickLoginBtn:(id)sender {
    
//    //离线登录
//    WKWelcomeViewController *welcome = [[WKWelcomeViewController alloc] init];
//    [self.navigationController pushViewController:welcome animated:YES];
    
    //在线登录
    [httpManager loginWithPhoneNum:self.phoneNum.text password:self.password.text success:^(NSDictionary *result) {
        
        NSDictionary *data = [result objectForKey:@"data"];
        if (data != nil) {
            
            WKLoginModel *loginModel = [WKLoginModel mj_objectWithKeyValues:data];
            DLog(@"loginModel=%ld--%ld--%@--%@",(long)loginModel.id,(long)loginModel.mobile,loginModel.name,loginModel.sessionToken);
            
            [[NSUserDefaults standardUserDefaults] setInteger:loginModel.id forKey:@"id"];
            [[NSUserDefaults standardUserDefaults] setInteger:loginModel.mobile forKey:@"mobile"];
            [[NSUserDefaults standardUserDefaults] setObject:loginModel.currencyType forKey:@"currencyType"];
            [[NSUserDefaults standardUserDefaults] setObject:loginModel.language forKey:@"language"];
            [[NSUserDefaults standardUserDefaults] setObject:loginModel.sessionToken forKey:@"sessionToken"];
            
            NSInteger value = [[NSUserDefaults standardUserDefaults] integerForKey:@"first"];
            if (value == 1) {
                WKRootViewController *root = [[WKRootViewController alloc] init];
                [self.navigationController pushViewController:root animated:YES];
            }
            else {
                [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"first"];
                WKWelcomeViewController *welcome = [[WKWelcomeViewController alloc] init];
                [self.navigationController pushViewController:welcome animated:YES];
            }
        
        }
        DLog(@"%@",result);
        NSDictionary *errors=[result objectForKey:@"errors"];
        NSString *message=[errors objectForKey:@"message"];
        if(message!=nil){
       
            UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
        
            [self presentViewController:alt animated:YES completion:nil];
        
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (IBAction)clickResetPasswordBtn:(id)sender {
    WKResetPasswordViewController *resetPassword = [[WKResetPasswordViewController alloc] init];
    resetPassword.topNumber=0;
    [self.navigationController pushViewController:resetPassword animated:YES];
}

- (IBAction)clickRegisterBtn:(id)sender {
    WKResetPasswordViewController *resetPassword = [[WKResetPasswordViewController alloc] init];
    resetPassword.topNumber=1;
    [self.navigationController pushViewController:resetPassword animated:YES];
}
//收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_phoneNum resignFirstResponder];
    [_password resignFirstResponder];
}
@end
